#pragma once

#pragma comment(lib, "d3dx9.lib")

#define DIRECTINPUT_VERSION 0x0800

#include <windows.h>
#include <mmsystem.h>
#include <dsound.h>
#include <d3d9types.h>
#include <d3d9.h>
#include <d3dx9tex.h>
#include <Dinput.h>
#include <string>
#include <dxerr.h>

#include "dsutil.h"

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

//Direct Input Key Actions
#define MOVE_LEFT	0x0001
#define MOVE_RIGHT	0x0002
#define DROP		0x0004
#define	FLIP		0x0008
#define	SWAP		0x0010
#define PAUSE		0x0200
#define SHOOT		0x0100


enum {RED, BLUE, GREEN, YELLOW, PURPLE, ORANGE, WHITE};

class DxManager
{
public:
	DxManager(void);
	~DxManager(void);
	bool init(HWND hwnd);
	void shutdown(void);
	void beginRender(void);
	void endRender(void);
	IDirect3DSurface9* loadSurfaceFromBitmap(std::string filename, int width, int height);
	LPDIRECT3DDEVICE9 getDevice() { return pd3dDevice; }
	IDirect3DSurface9* getBackBuffer(void);
	void blitToSurface(IDirect3DSurface9* srcSurface, const RECT *srcRect, const RECT *destRect);


	void shutdownDirectSound(void);
	LPDIRECTSOUNDBUFFER LoadWaveToSoundBuffer(std::string wavFilename);
	void stopSound(LPDIRECTSOUNDBUFFER whichBuffer);
	void playSoundLoop(LPDIRECTSOUNDBUFFER whichBuffer);
	void playSound(LPDIRECTSOUNDBUFFER whichBuffer);

	void printText(int x, int y, std::string text);

	int getScreenHeight() {return screen_height;}
	int getScreenWidth() {return screen_width;}

	void initDInput(HINSTANCE hInstance, HWND hWnd);
	void detect_keys(BYTE keystate[]);
	int readInput();											//Reads Keyboard and returns active flag
	DIMOUSESTATE detect_mousepos();//DIMOUSESTATE mousestate);      // gets the mouse movement and updates the static variables
	void cleanDInput(void);

private:
	HRESULT                 lastResult;
	LPDIRECT3D9             pD3D;
	LPDIRECT3DDEVICE9       pd3dDevice;
	LPDIRECTSOUND8          g_pDS;	    // The DirectSound Device
	LPDIRECTSOUNDBUFFER     DSBuffer;		

	BYTE kbKeys[256];					 //Keyboard Buffer
	int activeFlag;						 //flag for direct Input
	LPDIRECTINPUT8 din;                  //DirectInput Device
	LPDIRECTINPUTDEVICE8 dinkeyboard;    // Keyboard Device
	LPDIRECTINPUTDEVICE8 dinmouse;       // the pointer to the mouse device
	DIMOUSESTATE mousestate;


	ID3DXFont * m_font;

	ID3DXFont * font;

	D3DLIGHT9 light;

	// screen details
	int screen_width;
	int screen_height;

};
